#coding=utf-8
import os
import cv2
import  datetime

'''
封装ffmpeg的命令


'''



#重置尺寸
def resize(input_file,new_size,out_file):
    tempcmd = "ffmpeg  -i  {0}  -s  {1}  {2}".format(input_file,new_size, out_file)
    print '{0}'.format(tempcmd)
    os.popen3(tempcmd)
# 尺寸减半，新文件名添加后缀 _2
# 2分钟的视频，花费54秒，ffmpeg非常消耗cpu，调用期间cup达到99%
def resize_half(input_file):
    cap = cv2.VideoCapture(input_file)
    width =  int(cap.get(cv2.CAP_PROP_FRAME_WIDTH)/2 )
    height = int(cap.get(cv2.CAP_PROP_FRAME_HEIGHT)/2 )
    new_size = '{0}x{1}'.format(width,height)
    #get out_file
    filepath, tempfilename = os.path.split(input_file)
    short_name, extension = os.path.splitext(tempfilename)
    out_file = '{0}\\{1}_2{2}'.format(filepath,short_name,extension)
    resize(input_file,new_size,out_file)



if __name__ == '__main__':
    start_time = datetime.datetime.now()
    resize_half(r'D:\data\test\4\1.mp4')
    end_time = datetime.datetime.now()
    print  "total %s" % (end_time - start_time).total_seconds()